from threading import Thread

import redis

from dht.client import Client
from dht.table import KTable
from dht.utils import random_id


class modle(object):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.handle = redis.Redis(host=self.host, port=self.port, db=0)

    def add_ip(self, target):
        self.handle.sadd("ip_set",target)

    def add_hash(self, target):
        self.handle.sadd("infohash", target)


if __name__ == '__main__':
    m = modle('127.0.0.1', 6379)
    client = Client(9565, KTable(random_id()), m)
    threads = [
        # Thread(target=client.send_find_node_forever),
        Thread(target=client.receive_response_forever),
        Thread(target=client.refresh_table),
        Thread(target=client.get_sizes),
        Thread(target=client.back),
    ]

    for t in threads:
        t.start()

    for t in threads:
        t.join()
    # client.start()
